<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never"></el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()">添加广告</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="bannerTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="120" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="广告图片" width="250" align="center">
          <template slot-scope="scope">
            <img style="height: 80px" :src="scope.row.picUrl" />
          </template>
        </el-table-column>
        <el-table-column label="展示区域" width="500" align="center">
          <template slot-scope="scope">{{scope.row.isZuoti | formatArea}}</template>
        </el-table-column>
        <el-table-column label="广告排序" align="center">
          <template slot-scope="scope">{{scope.row.orderby}}</template>
        </el-table-column>
        <el-table-column label="添加时间" width="200" align="center">
          <template slot-scope="scope">{{scope.row.createDatetime | formatTime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdate(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>``
<script>
import { fetchList, deleteBanner } from "@/api/dms/drivermanage";
import { formatDate } from "@/utils/date";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 5
};
export default {
  name: "bannerList",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: []
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      var n = time.replace("-","/");
      n = n.replace("-","/");
      let date = new Date(n);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
    formatArea(isZuoti) {
      if (isZuoti==0) {
        return "首页轮播";
      }
      if (isZuoti==1){
        return "门店轮播"
      }
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleDelete(index, row) {
      this.$confirm("是否要删除该条记录", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteBanner(row.id).then(response => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      });
    },
    handleAdd() {
      this.$router.push({ path: "/drivermanage/addBanner" });
    },
    handleUpdate(index, row) {
      this.$router.push({
        path: "/drivermanage/updateBanner",
        query: { id: row.id }
      });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.records;
        this.total = response.data.total;
      });
    }
  }
};
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>


